package com.project.controller;

import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("login")
    public String login(String userName, String pwd, HttpServletRequest request){
        UserBean userBean = service.login(userName,pwd);
        if (userBean != null){
            request.getSession().setAttribute("loginUser",userBean);
            return "ok";
        }
        return "no";
    }

    @RequestMapping("getUserName")
    public UserBean getUserName(HttpServletRequest request){
        UserBean userBean =(UserBean)request.getSession().getAttribute("loginUser");
        System.out.println(userBean);
        return userBean;
    }

    @RequestMapping("register")
    public String register(String name, String pwd, String newPwd,
                           String email, String face,HttpServletRequest request){
        UserBean userBean = service.findName(name);
        if (userBean == null){
            if (pwd.equals(newPwd)){
                UserBean user = new UserBean(name,pwd,email,face,null);
                service.add(user);
                return "ok";
            }
        }
        return "no";
    }
}
